#include "spl.includes.h"
#include "splib.h"

using namespace SPLib;
using namespace SPLib::IO;

BOOL IO::ReadBuffer(HANDLE iFile, PVOID iBuffer, SIZE_T iSize)
{
  DWORD wRead = 0;
  DWORD wReadTotal = 0;

  while (wReadTotal < iSize)
  {
    if (!ReadFile(
      iFile,
      (BYTE*)iBuffer + wReadTotal,
      (DWORD)(iSize - wReadTotal),
      &wRead,
      NULL) || !wRead)
    {
      break;
    }

    wReadTotal += wRead;
  }

  return wReadTotal == iSize;
}

BOOL IO::WriteBuffer(HANDLE iFile, PVOID iBuffer, SIZE_T iSize)
{
  DWORD wWritten      = 0;
  DWORD wWrittenTotal = 0;

  while (wWrittenTotal < iSize)
  {
    if (!WriteFile(
      iFile,
      (BYTE*)iBuffer + wWrittenTotal,
      (DWORD)(iSize - wWrittenTotal),
      &wWritten,
      NULL) || !wWritten)
    {
      break;
    }

    wWrittenTotal += wWritten;
  }

  return wWrittenTotal == iSize;
}
